package br.com.tiespec.ejb.Dao;

import javax.ejb.Stateless;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import br.com.tiespec.model.Modelo.ClienteProjeto;

@Stateless(mappedName = "ClienteProjetoDAO")
public class ClienteProjetoDAO extends DAO {

	private static final long serialVersionUID = 1L;

	public Integer countClientesDeUmCliente(Long idClientePortal) {
		Integer count = 0;
		try {
			begin();
			Criteria criteria = this.session.createCriteria(ClienteProjeto.class)
					.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
			       	.setProjection(Projections.rowCount())
					.add(Restrictions.eq("clientePortal.id", idClientePortal));
			
			count = Integer.parseInt(criteria.uniqueResult().toString());
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return count;
	}
	
	public ClienteProjeto selecionar(Long id) {
		ClienteProjeto cpInstance = new ClienteProjeto();
		try {
			begin();
			cpInstance = (ClienteProjeto) this.session.createCriteria(ClienteProjeto.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			commit();
			fecharConexao();
		} catch (Exception e){
			e.printStackTrace();
			fecharConexao();
		}
		return cpInstance;
	}
	
}